﻿#region Using directives
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using gpsMe.Classes;
using gpsMe.GPS;
using gpsMe.Utils;
#endregion

namespace gpsMe.UI.Forms
{
    /// <summary>
    /// Gps Logger Extra
    /// </summary>
    public partial class gpsMeExtraGpsLoggerForm : Form
    {
        #region Instance
        /// <summary>
        /// Current gpsMeExtraGpsLoggerForm Instance
        /// </summary>
        public static gpsMeExtraGpsLoggerForm Instance { get; private set; }
        #endregion

        #region Attributes
        /// <summary>
        /// Specifies if the form is listenning the GPS logger
        /// </summary>
        private bool isListenning = true;

        /// <summary>
        /// Buffer that allows the form to keep listenning the GPS logger while not writing in textbox
        /// </summary>
        private string pauseTextBuffer = "";
        #endregion

        #region Constructor
        /// <summary>
        /// Default constructor
        /// </summary>
        public gpsMeExtraGpsLoggerForm()
        {
            Instance = this;
            InitializeComponent();
            gpsMeGpsLogger.Instance.TextChanged += new EventHandler(gpsMeGpsLogger_TextChanged);
            this.logTextBox.Text = gpsMeGpsLogger.Instance.Text;
            if (gpsMeGpsProvider.Instance == null)
                new gpsMeGpsProvider();
            this.Closing += new CancelEventHandler(gpsMeExtraGpsLoggerForm_Closing);
        }
        #endregion

        #region Methods / Functions / Events
        /// <summary>
        /// Invoked when the close button is clicked
        /// </summary>
        /// <param name="p_oSender"></param>
        /// <param name="p_oEventArgs"></param>
        private void closeButton_Click(object p_oSender, EventArgs p_oEventArgs)
        {
            this.Close();
        }

        /// <summary>
        /// Invoked when the logger text has changed
        /// </summary>
        /// <param name="p_oSender"></param>
        /// <param name="p_oEventArgs"></param>
        private void gpsMeGpsLogger_TextChanged(object p_oSender, EventArgs p_oEventArgs)
        {
            BeginInvoke(new gpsMeUtils.MethodInvoker(delegate()
            {
                if (this.isListenning)
                    this.logTextBox.Text += gpsMeGpsLogger.Instance.Text;
                else
                    this.pauseTextBuffer += gpsMeGpsLogger.Instance.Text;
            }));
        }

        /// <summary>
        /// Invoked when the form is closing
        /// </summary>
        /// <param name="p_oSender"></param>
        /// <param name="p_oEventArgs"></param>
        private void gpsMeExtraGpsLoggerForm_Closing(object p_oSender, CancelEventArgs p_oEventArgs)
        {
            gpsMeGpsLogger.Instance.TextChanged -= new EventHandler(gpsMeGpsLogger_TextChanged);
        }

        /// <summary>
        /// Invoked when the pauseResume button is clicked
        /// </summary>
        /// <param name="p_oSender"></param>
        /// <param name="p_oEventArgs"></param>
        private void PauseResumeButton_Click(object p_oSender, EventArgs p_oEventArgs)
        {
            if (this.isListenning)
                this.pauseResumeButton.Text = "Resume listenning";
            else
                this.pauseResumeButton.Text = "Pause listenning";
            this.isListenning = !this.isListenning;
            if (this.isListenning)
            {
                this.logTextBox.Text += this.pauseTextBuffer;
                this.pauseTextBuffer = "";
            }
        }
        #endregion
    }
}